<?php
class Controller_User extends Controller {
	
	
	
    function action_index()
    {
       $this->request->response = new view('user/register') ;
    }
 
    public function action_register()
    {
        $user = Model::factory('user');
 
        $post = Validate::factory($_POST)
            ->filter(TRUE, 'trim')
 
            ->filter('username', 'strtolower')
 
            ->rule('username', 'not_empty')
            ->rule('username', 'regex', array('/^[a-z_.]++$/iD'))
            ->rule('username', array($user, 'unique_username'))
 
            ->rule('password', 'not_empty')
            ->rule('password', 'min_length', array('6'))
            ->rule('confirm',  'matches', array('password'))
 
            ->rule('use_ssl', 'not_empty')
            ->rule('use_ssl', 'in_array', array(array('yes', 'no')))
 
            ->callback('password', array($user, 'hash_password'));
 
        if ($post->check())
        {
            // Data has been validated, register the user
            $user->register($post);
 
            // Always redirect after a successful POST to prevent refresh warnings
            URL::redirect('user/profile');
        }
 
        // Validation failed, collect the errors
        $errors = $post->errors('user');
 
        // Display the registration form
        $this->request->response = View::factory('user/register')
            ->bind('post', $post)
            ->bind('errors', $errors);
    }
 
}
?>